package com.cbw.firegame.order.web;

import com.alibaba.fastjson.TypeReference;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cbw.common.utils.R;
import com.cbw.common.vo.*;
import com.cbw.firegame.order.config.AlipayTemplate;
import com.cbw.firegame.order.entity.OrderDescEntity;
import com.cbw.common.vo.OrderEntity;
import com.cbw.firegame.order.feign.ProductServiceFeign;
import com.cbw.firegame.order.service.OrderDescService;
import com.cbw.firegame.order.service.OrderService;
import com.cbw.firegame.order.vo.PayVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class PayWebController {
    @Autowired
    AlipayTemplate alipayTemplate;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDescService orderDescService;
    @Autowired
    ProductServiceFeign productServiceFeign;
@ResponseBody
@GetMapping( value = "/payOrder",produces = "text/html")
public String payOrder(@RequestParam("orderId")String orderId) throws AlipayApiException {
    PayVo payVo = new PayVo();
    OrderDescEntity order = orderDescService.getOne(new QueryWrapper<OrderDescEntity>().eq("order_id", orderId));
    BigDecimal bigDecimal = order.getPrice().setScale(2, BigDecimal.ROUND_UP);
    payVo.setOut_trade_no(orderId);
    payVo.setSubject(order.getGameName());
    payVo.setTotal_amount(bigDecimal.toString());
    payVo.setBody("你正在购买"+order.getGameName()+"款游戏");
    String pay = alipayTemplate.pay(payVo);
    return pay;
}

    @GetMapping( "/paysuccess")
    public String paysuccess(HttpSession httpSession, Model model){
        MemberEntity loginUser = (MemberEntity) httpSession.getAttribute("loginUser");

        R allCategory = productServiceFeign.getAllCategory();
        if (allCategory.getCode() == 0) {
            List<CategoryEntity> data = allCategory.getData(new TypeReference<List<CategoryEntity>>() {});
            System.out.println(data);
            model.addAttribute("categorys", data);
        }

        OrderEntity order = orderService.getNewOrder(loginUser.getId());
        order.setStatus(1);
        orderService.updateById(order);

        OrderDescEntity orderDesc = orderDescService.getOne(new QueryWrapper<OrderDescEntity>().eq("order_id", order.getOrderId()));
        GameEntity updateGame = productServiceFeign.getGame(orderDesc.getGameId());
        updateGame.setSaleCount(updateGame.getSaleCount()+1);
        productServiceFeign.update(updateGame.getGameId(),updateGame);

        //查询玩家所有已经支付游戏
        List<OrderEntity> list = orderService.list(new QueryWrapper<OrderEntity>().eq("member_id", loginUser.getId())
                .eq("status", 1));
        List<OrderDescEntity> descList = list.stream().map(item -> {
            OrderDescEntity order_id = orderDescService.getOne(new QueryWrapper<OrderDescEntity>().eq("order_id", item.getOrderId()));

            return order_id;
        }).collect(Collectors.toList());

        List<GameEntityVo> collect = descList.stream().map(item -> {
            GameEntity game = productServiceFeign.getGame(item.getGameId());
            R r = productServiceFeign.gameDetailinfo(game.getGameId());
            GameInfoEntity gameInfo = r.getData("gameInfo", new TypeReference<GameInfoEntity>() {
            });
            GameEntityVo gameEntityVo = new GameEntityVo();
            BeanUtils.copyProperties(game, gameEntityVo);
            gameEntityVo.setDataSouce(gameInfo.getGameDatasource());
            return gameEntityVo;
        }).collect(Collectors.toList());


        model.addAttribute("orderGame",collect);


        return "mygame";
    }


    @GetMapping( "/tomygame")
    public String tomygame(HttpSession httpSession, Model model){
        MemberEntity loginUser = (MemberEntity) httpSession.getAttribute("loginUser");

        R allCategory = productServiceFeign.getAllCategory();
        if (allCategory.getCode() == 0) {
            List<CategoryEntity> data = allCategory.getData(new TypeReference<List<CategoryEntity>>() {});
            System.out.println(data);
            model.addAttribute("categorys", data);
        }

        //查询玩家所有已经支付游戏
        List<OrderEntity> list = orderService.list(new QueryWrapper<OrderEntity>().eq("member_id", loginUser.getId())
                .eq("status", 1));
        List<OrderDescEntity> descList = list.stream().map(item -> {
            OrderDescEntity order_id = orderDescService.getOne(new QueryWrapper<OrderDescEntity>().eq("order_id", item.getOrderId()));

            return order_id;
        }).collect(Collectors.toList());

        List<GameEntityVo> collect = descList.stream().map(item -> {
            GameEntity game = productServiceFeign.getGame(item.getGameId());
            R r = productServiceFeign.gameDetailinfo(game.getGameId());
            GameInfoEntity gameInfo = r.getData("gameInfo", new TypeReference<GameInfoEntity>() {
            });
            GameEntityVo gameEntityVo = new GameEntityVo();
            BeanUtils.copyProperties(game, gameEntityVo);
            gameEntityVo.setDataSouce(gameInfo.getGameDatasource());


            return gameEntityVo;
        }).collect(Collectors.toList());
        model.addAttribute("orderGame",collect);


        return "mygame";
    }




}
